package com.sky.mapper;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);

    @Select("select * from sky_take_out.orders where user_id = #{currentId}")
    List<Orders> queryByUserId(Long currentId);

    @Select("select * from sky_take_out.orders where id = #{id}")
    Orders queryByOrderId(Long id);

    void updateOrder(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    List<Orders> queryByConditions(OrdersPageQueryDTO dto);

    Integer count(Integer status);

    void updateStatus(Long id ,Integer status);

    @Update("update orders set status = #{orderStatus} ,pay_status = #{orderPaidStatus} , checkout_time = #{checkOutTime} where number =#{orderNumber}")
    void updateStatusForWechat(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, String orderNumber);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime time);

    @Select("select * from orders where order_time between #{begin} and #{end}")
    List<Orders> queryByDateBetween(LocalDate begin, LocalDate end);

    @Select("select sum(amount) from sky_take_out.orders where order_time > #{begin} and order_time < #{end} and status = 5")
    BigDecimal countTurnoverInDay(LocalDateTime begin , LocalDateTime end);
}
